package com.xizi.lotterySystem.dao.handler;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(Encrypt.class)
public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {
    // 使用 UTF-8 编码确保多语言支持
    private static final byte[] KEYS = "2005110530405002".getBytes(StandardCharsets.UTF_8);

    private AES getAes() {
        return SecureUtil.aes(KEYS);
    }

    // 解密方法
    private String decrypt(String encryptedValue) {
        if (!StringUtils.hasText(encryptedValue)) {
            return null;
        }
        return getAes().decryptStr(encryptedValue);
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i,
                                    Encrypt parameter, JdbcType jdbcType) throws SQLException {
        if (parameter == null || !StringUtils.hasText(parameter.getValue())) {
            ps.setString(i, null);
            return;
        }
        String encryptHex = getAes().encryptHex(parameter.getValue());
        ps.setString(i, encryptHex);
    }

    @Override
    public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : new Encrypt(decrypt(value));
    }

    @Override
    public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : new Encrypt(decrypt(value));
    }

    @Override
    public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : new Encrypt(decrypt(value));
    }
}















//@MappedJdbcTypes(JdbcType.VARCHAR)
//@MappedTypes(Encrypt.class)
//public class EncryptTypeHandler extends BaseTypeHandler<Encrypt> {
//    // 对称加密
//    private static final byte[] KEYS = "2005110530405002".getBytes(StandardCharsets.UTF_8);
//
//    //解密
//    private static Encrypt decrypt(String encryptStr) {
//        return new Encrypt(SecureUtil.aes(KEYS).decryptStr(encryptStr));
//    }
//
//    // 加密
//    @Override
//    public void setNonNullParameter(PreparedStatement ps, int i, Encrypt parameter, JdbcType jdbcType) throws SQLException {
//        if (!StringUtils.hasText(parameter.getValue())){
//            ps.setString(i, null);
//            return;
//        }
//        AES aes = SecureUtil.aes(KEYS);
//        String encryptHex = aes.encryptHex(parameter.getValue());
//        ps.setString(i, encryptHex);
//    }
//
//    @Override
//    public Encrypt getNullableResult(ResultSet rs, String columnName) throws SQLException {
//        if(rs.getString(columnName) == null){
//            return null;
//        }
//        return decrypt(rs.getString(columnName));
//    }
//
//    @Override
//    public Encrypt getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
//        if(rs.getString(columnIndex) == null){
//            return null;
//        }
//        return decrypt(rs.getString(columnIndex));
//    }
//
//    @Override
//    public Encrypt getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
//        if(cs.getString(columnIndex) == null){
//            return null;
//        }
//        return decrypt(cs.getString(columnIndex));
//    }
//}
